import pytest
from playwright.sync_api import Browser

from page.login_page import LoginPage


@pytest.fixture(scope="session",autouse=True)
def login_save_auth(browser:Browser,base_url,pytestconfig):
    """登录 保存cookies"""
    context = browser.new_context(base_url=base_url)
    page = context.new_page()
    LoginPage(page).navigate()
    LoginPage(page).login("test","123456")

    page.wait_for_url("**/index.html")
    storage_path = pytestconfig.rootpath.joinpath("auth/state.json")
    context.storage_state(path=storage_path)
    context.close()

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright, pytestconfig):
    """
    添加context 上下文参数，默认每个页面加载cookies
    :param browser_context_args:
    :param playwright:
    :return:
    """
    return {
        "storage_state":pytestconfig.rootpath.joinpath("auth/state.json"),**browser_context_args,
    }